package dao.impl;

import dao.BaseDao;
import dao.PersonDao;
import entity.Person;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PersonDaoImpl extends BaseDao implements PersonDao {
    @Override
    public boolean delPerson(int pid) throws SQLException, ClassNotFoundException {
        getConnection();
        boolean flag = false;
        String sql = "DELETE FROM person WHERE id = ?";
        PreparedStatement pt = null;
        try {
            pt = conn.prepareStatement(sql);
            pt.setInt(1, pid);
            // 执行删除操作，返回受影响的行数
            int rowsAffected = pt.executeUpdate();
            if (rowsAffected > 0) {
                // 如果受影响行数大于 0，说明删除成功，更新标志
                flag = true;
            }
        } finally {
            // 关闭资源
            if (pt != null) {
                try {
                    pt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }

    @Override
    public List<Person> getPersonList() throws SQLException, ClassNotFoundException {
        getConnection();
        List <Person> list =new ArrayList<>();
        String sql="select * from person";
        PreparedStatement pst=conn.prepareStatement(sql);
        ResultSet rs=pst.executeQuery();
        while (rs.next()){
            Person p=new Person();
            p.setId(rs.getInt("id"));
            p.setName(rs.getString("name"));
            p.setSex(rs.getString("sex"));
            p.setAge(rs.getInt("age"));
            p.setRank(rs.getString("rank"));
            p.setDepartment(rs.getString("department"));
            list.add(p);
        }
        return list;
    }

    @Override
    public boolean addPerson(String name, String sex, int age, String rank, String department) throws SQLException, ClassNotFoundException {
        getConnection();
        String sql = "INSERT INTO `hrinfo`.`person` ( `name`, `sex`, `age`, `rank`, `department`) VALUES (?,?,?,?,?);";
        PreparedStatement a = conn.prepareStatement(sql);
        a.setString(1, name);
        a.setString(2, sex);
        a.setInt(3, age);
        a.setString(4, rank);
        a.setString(5, department);
        int rows = a.executeUpdate();
        if (rows > 0) {
            return true;
        }

        return false;
    }

    @Override
    public boolean updatePerson(String name, String sex, Integer age, String rank, String department, Integer id) throws SQLException, ClassNotFoundException {
        getConnection();
        String sql = "UPDATE person SET name = ?,sex = ?,age = ?,rank = ?,department = ? WHERE id = ?";
        PreparedStatement p = conn.prepareStatement(sql);
        p.setString(1,name);
        p.setString(2,sex);
        p.setInt(3,age);
        p.setString(4,rank);
        p.setString(5,department);
        p.setInt(6,id);
        return p.executeUpdate()>0;
    }
}
